package com.zhuoyue.biz.core.command.cmd.common.fad;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.command.model.BaseParams;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import com.zhuoyue.common.utils.CmdUtils;
import lombok.Data;

import java.nio.ByteBuffer;

/**
 * @author wjming
 * @ClassName: AirConditioningGateway
 * @Description: 空调网关控制器
 * @Date 2021-08-02 14:16
 */
public class AirConditioningGateway {
    private static final String AC_CMD_MODE = "0201${gatewayMac}${deviceMac}0FADFF1200003800${address}${type}${switchState}${mode}${windSpeed}${temperature}";
    /**
     * 开关控制
     */
    private static final String TYPE_SWITCH = "01";
    /**
     * 模式控制
     */
    private static final String TYPE_MODE = "02";
    /**
     * 风速控制
     */
    private static final String TYPE_FAN_SPEED = "03";
    /**
     * 温度控制
     */
    private static final String TYPE_TEMPERATURE = "04";
    /**
     * 全控配置
     */
    private static final String SET_ALL = "05";

    /**
     * 修改所有
     */
    private static final String UPDATE_ALL = "06";
    /**
     * 开
     */
    private static final String SWITCH_OPEN = "01";
    /**
     * 关
     */
    private static final String SWITCH_CLOSE = "02";


    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String address = (String) controlReq.getActions().get("acAddress");
        int outAcAddress = 0;
        int innerAcAddress = 0;
        if (!ControlType.UPDATE.equals(controlReq.getControlType())) {
            String[] s = address.split("_");
            outAcAddress = Integer.parseInt(s[0]);
            innerAcAddress = Integer.parseInt(s[1]);
        }

        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();
        switch (controlReq.getControlType()) {
            //打开空调
            case ControlType.TURN_ON_REQUEST:
                return new AirConditioningGatewayCmd(gatewayMac, deviceMac, AirAddress.returnAddress16(outAcAddress, innerAcAddress),
                        TYPE_SWITCH, SWITCH_OPEN, "00", "00", "0000").getCmd(AC_CMD_MODE);
            //关闭空调
            case ControlType.TURN_OFF_REQUEST:
                return new AirConditioningGatewayCmd(gatewayMac, deviceMac, AirAddress.returnAddress16(outAcAddress, innerAcAddress),
                        TYPE_SWITCH, SWITCH_CLOSE, "00", "00", "0000").getCmd(AC_CMD_MODE);
            //设置模式
            case ControlType.SET_MODE_REQUEST:
                String modeState = controlReq.getActions().get("modeState") == null ? ControlType.SetMode.COLD : (String) controlReq.getActions().get("modeState");
                String mode = mode(modeState);
                return new AirConditioningGatewayCmd(gatewayMac, deviceMac, AirAddress.returnAddress16(outAcAddress, innerAcAddress),
                        TYPE_MODE, "00", mode, "00", "0000").getCmd(AC_CMD_MODE);
            //设置风速
            case ControlType.SET_FAN_SPEED_REQUEST:
                String windRankState = controlReq.getActions().get("windRankState") == null ? ControlType.SetWindSpeed.MID : (String) controlReq.getActions().get("windRankState");
                String wind = windRank(windRankState);
                return new AirConditioningGatewayCmd(gatewayMac, deviceMac, AirAddress.returnAddress16(outAcAddress, innerAcAddress),
                        TYPE_FAN_SPEED, "00", "00", wind, "0000").getCmd(AC_CMD_MODE);
            //设置温度
            case ControlType.SET_TEMPERATURE_REQUEST:
                String temperature = controlReq.getActions().get("temperature") == null ? "26" : (String) controlReq.getActions().get("temperature");
                String hexString = CmdUtils.getHexString(Integer.parseInt(temperature) * 10);

                return new AirConditioningGatewayCmd(gatewayMac, deviceMac, AirAddress.returnAddress16(outAcAddress, innerAcAddress),
                        TYPE_TEMPERATURE, "00", "00", "00", hexString).getCmd(AC_CMD_MODE);
            //设置所有信息
            case ControlType.SET_ALL:
                modeState = controlReq.getActions().get("modeState") == null ? ControlType.SetMode.COLD : (String) controlReq.getActions().get("modeState");
                mode = mode(modeState);
                windRankState = controlReq.getActions().get("windRankState") == null ? ControlType.SetWindSpeed.MID : (String) controlReq.getActions().get("windRankState");
                wind = windRank(windRankState);
                temperature = controlReq.getActions().get("temperature") == null ? "26" : (String) controlReq.getActions().get("temperature");
                hexString = CmdUtils.getHexString(Integer.parseInt(temperature) * 10);
                return new AirConditioningGatewayCmd(gatewayMac, deviceMac, AirAddress.returnAddress16(outAcAddress, innerAcAddress),
                        SET_ALL, SWITCH_OPEN, mode, wind, hexString).getCmd(AC_CMD_MODE);
            case ControlType.UPDATE:
                return new AirConditioningGatewayCmd(gatewayMac, deviceMac, "FFFFFFFFFFFFFFFF",
                        UPDATE_ALL, "00", "00", "00", "0000").getCmd(AC_CMD_MODE);
            default:
                throw new ZyServerException(ErrorType.Service_Fail.getCode(), "没有此控制类型");
        }
    }

    /**
     * 模式
     *
     * @return
     */
    private static String mode(String modeState) {
        switch (modeState) {
            //制冷
            case ControlType.SetMode.COOL:
            case ControlType.SetMode.COLD:
                return "03";
            //制热
            case ControlType.SetMode.HEAT:
                return "04";
            //除湿
            case ControlType.SetMode.DEHUMIDIFICATION:
                return "05";
            //送风
            case ControlType.SetMode.WIND:
                return "06";
            default:
                throw new ZyServerException(ErrorType.Service_Fail.getCode(), "控制模式控制不正确");
        }
    }

    /**
     * 风速
     *
     * @return
     */
    public static String windRank(String windRankState) {
        switch (windRankState) {
            //低风速
            case ControlType.SetWindSpeed.LOW:
                return "02";
            //中风速
            case ControlType.SetWindSpeed.MID:
                return "03";
            //高风速
            case ControlType.SetWindSpeed.HIGH:
                return "04";
            default:
                break;
        }
        return null;
    }

}

@Data
class AirConditioningGatewayCmd extends BaseParams {
    /**
     * 空调对应的地址
     */
    private String address;
    /**
     * 控制类型
     */
    private String type;
    /**
     * 开关状态
     */
    private String switchState;
    /**
     * 空调模式
     */
    private String mode;
    /**
     * 风速度
     */
    private String windSpeed;
    /**
     * 温度
     */
    private String temperature;

    public AirConditioningGatewayCmd(String gatewayMac, String deviceMac, String address, String type, String switchState, String mode, String windSpeed, String temperature) {
        super(gatewayMac, deviceMac);
        this.address = address;
        this.type = type;
        this.switchState = switchState;
        this.mode = mode;
        this.windSpeed = windSpeed;
        this.temperature = temperature;
    }
}

/**
 * 地址计算
 */
class AirAddress {
    /**
     * @param outAcAddress   空调外机地址
     * @param innerAcAddress 空调内机地址
     */
    public static String returnAddress16(int outAcAddress, int innerAcAddress) {
        //真实的空调地址
        int s = (outAcAddress - 1) * 16 + (innerAcAddress);
        long address = 1 << s;
        return CmdUtils.intTo16(address);
    }

}




